-- PwrdTrackEvent
-- 完美SDK埋点
local PwrdTrackEvent = class("PwrdTrackEvent")


function PwrdTrackEvent:ctor()
    if app:getPublisher() ~= Enums.Publisher.pwrd and not ENABLE_DEBUG_ADREPORD then
        return
    end

    -- 创角成功埋点
    EM:on("OnRoleCreated", function(event, uid, zoneData)
        self:addTrackEvent({eventName = "Create_role", roleId = tostring(uid), type = Enums.PwrdTrackEventType.Role}) -- 完美客户端打点
        self:addTrackEvent({eventName = "Create_role", roleId = tostring(uid), type = Enums.PwrdTrackEventType.AD}) -- 广告打点
    end)
    -- 登录成功埋点(loading完之后)
    EM:on("onMapChanged", function(event, isFirstEnterMap)
        if isFirstEnterMap then
            self:addTrackEvent({eventName = "roleLoginSDK", type = Enums.PwrdTrackEventType.Role}) -- 完美客户端打点
            self:addTrackEvent({eventName = "RoleLogin", type = Enums.PwrdTrackEventType.AD}) -- 广告打点
            self:onLoginDaysTrackEvent() -- 累计登录广告打点
        end
    end)
    -- 登录失败埋点
    EM:on("onRoleLoginFailed", function(event)
        self:addTrackEvent({eventName = "roleLoginErrorSDK", type = Enums.PwrdTrackEventType.Role})
    end)
    --角色断网重连成功埋点
    EM:on("onReconnectSuccessed", function(event)
        self:addTrackEvent({eventName = "roleReconnection", type = Enums.PwrdTrackEventType.WanMei})
    end)
    -- 角色升级埋点
    EM:on("onPlayerLevelChange", function(name, level)
        self:addTrackEvent({eventName = "roleUpdateSDK", type = Enums.PwrdTrackEventType.Role}) -- 完美客户端打点
        self:addTrackEvent({eventName = "Level"..level, type = Enums.PwrdTrackEventType.AD}) -- 广告打点
    end)
    -- 角色登出埋点
    EM:on("OnRoleDisable", function(event)
        self:addTrackEvent({eventName = "roleLogoutSDK", type = Enums.PwrdTrackEventType.Role}) -- 完美客户端打点
        self:addTrackEvent({eventName = "RoleLogout", type = Enums.PwrdTrackEventType.AD}) -- 广告打点
    end)
    -- 拉取服务器列表埋点
    EM:on("onFetchRange", function(event, data)
        if data then
            self:wanmeiGameEvent(data)
        end
    end)
    -- 第一次充值埋点
    EM:on("onGetFirstRechangeRewardSuccess", function()
        self:addTrackEvent({eventName = "First_pay", type = Enums.PwrdTrackEventType.AD})
    end)
    -- 成功加入公会埋点
    EM:on("GuildUpdate", function()
        self:addTrackEvent({eventName = "Join Group", type = Enums.PwrdTrackEventType.AD})
    end)
    -- 成功创建公会埋点
    EM:on("OnGuildCreated", function()
        self:addTrackEvent({eventName = "Create Group", type = Enums.PwrdTrackEventType.AD})
    end)
    -- 战力埋点
    EM:on("onCombatPowerChange", function(name, combatPower)
        self:onCombatPowerTrackEvent(combatPower)
    end)
end

function PwrdTrackEvent:onLoginDaysTrackEvent()
    local loginDays = app.session:getLoginDays()
    local eventName = nil
    if loginDays == 2 or loginDays == 7 or loginDays == 15 or loginDays == 30 then
        eventName = loginDays.."d_retention"
    end
    if eventName ~= nil then
        self:addTrackEvent({eventName = eventName, type = Enums.PwrdTrackEventType.AD})
    end
end

function PwrdTrackEvent:onCombatPowerTrackEvent(combatPower)
    if not combatPower then
        combatPower = UD:getCombatPower()
    end
    local eventName
    if combatPower >= 500000 then
        local isRecorded = UD:getValueFromGenericByKey("cus_GS_03") -- 战力达到50万
        if not isRecorded then
            eventName = "cus_GS_03"
        end
    elseif combatPower >= 300000 then
        local isRecorded = UD:getValueFromGenericByKey("cus_GS_02") -- 战力达到30万
        if not isRecorded then
            eventName = "cus_GS_02"
        end
    elseif combatPower >= 200000 then
        local isRecorded = UD:getValueFromGenericByKey("cus_GS_01") -- 战力达到30万
        if not isRecorded then
            eventName = "cus_GS_02"
        end
    end
    if eventName then
        self:addTrackEvent({eventName = eventName, type = Enums.PwrdTrackEventType.AD})
        UD:saveGeneric(eventName, true)
    end
end

-- info = {eventName = "RoleLogin", type = Enums.PwrdTrackEventType.Role}
function PwrdTrackEvent:addTrackEvent(info)
    if not info then
        return
    end
    local list = {}
    local list = {
        eventName = info.eventName,
        roleId = info.roleId or UD.player.uid,
        serverId = tostring(app.session:getZoneId()),
        vip = tostring(0), -- 目前没有VIP的概念，先传0
        level = UD.player.level,
    }
    local _, host, port = string.parseUrl(app.session:getHosts())
    list.ip = host
    list.port = port

    if info.type == Enums.PwrdTrackEventType.Role then
        app.channelAPI:roleTrackEvent(list)
    elseif info.type == Enums.PwrdTrackEventType.WanMei then
        app.channelAPI:wanmeiTrackEvent(list)
    elseif info.type == Enums.PwrdTrackEventType.AD then
        app.channelAPI:trackEventAD(list)
    end
end

-- info = {stateIndex = Enums.PwrdTrackEventState.Begin, errorMsg = "网络错误"/nil}
function PwrdTrackEvent:wanmeiGameEvent(info)
    if not info then
        return
    end
    local list = {
        eventName = "GetServerList",
        stateIndex = info.stateIndex,
        errorMsg = info.errorMsg or -1, -- 如果没有传-1
        url = app.channelAPI:getGateHostFromConfig(),
    }
    app.channelAPI:wanmeiGameEvent(list)
end

return PwrdTrackEvent